package javaIo;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by zzy on 2018/4/2.
 */
public class BufferDemo {

    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream(new File("D:\\html\\FDG\\index.html"));
        FileChannel fc=fileInputStream.getChannel();

        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
        int dataCount=0;
        while (dataCount!=-1){

            dataCount=fc.read(byteBuffer);
            byteBuffer.position();
            byteBuffer.capacity();
           System.out.println(byteBuffer.limit()); byteBuffer.limit();
            byteBuffer.flip();//重置position，并将limit（）设置到有用区最后一个字节
           // System.out.println(new String(byteBuffer.array(),"UTF-8"));
        }
        fc.close();
        byteBuffer.flip();

        //文件映射(修改也映射)
        RandomAccessFile file=new RandomAccessFile("D:\\html\\FDG\\index.html","rw");
        FileChannel fileChannel=file.getChannel();
        MappedByteBuffer mbb=fileChannel.map(FileChannel.MapMode.READ_WRITE,0,file.length());
        while (mbb.hasRemaining()){
            System.out.println((char)mbb.get());
        }
        mbb.put(0, (byte) 97);
        file.close();
    }
}
